package com.rylan.controller;

import com.rylan.entity.*;
import com.rylan.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author rylan
 * @description
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderFeign orderFeign;


    @GetMapping("/save/{rid}")
    public String save(@PathVariable("rid")long rid, HttpSession httpSession){
        User user = (User) httpSession.getAttribute("user");
        Order order = new Order();
        order.setUser(user);
        Room room = new Room();
        room.setId(rid);
        order.setRoom(room);
        order.setState(0);
        order.setDate(new Date());
        orderFeign.save(order);
        return "redirect:/order/mahjong/order";
    }
    @GetMapping("/findAllByUid")
    @ResponseBody
    public OrderVO findAllByUid(@RequestParam("page") int page,@RequestParam("limit") int limit,HttpSession httpSession){
        User user =(User) httpSession.getAttribute("user");
        long uid = user.getId();
        int index = (page-1)*limit;
        List<Order> data = orderFeign.findAllByUid(uid,index,limit);
        int count = orderFeign.findAllByUid(uid,0,1000).size();
        return new OrderVO(0,"",count,data);
    }
    @GetMapping("/mahjong/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }

    @GetMapping("/updateState/{id}/{state}")
    public String updateState(@PathVariable("id") long id,@PathVariable("state") int state,HttpSession session){
        Admin admin = (Admin) session.getAttribute("admin");
        orderFeign.updateState(id,state,admin.getId());
        return "redirect:/order/mahjong/order_handler";
    }

    @GetMapping("/findAllByState")
    @ResponseBody
    public OrderVO findAllByState(@RequestParam("page") int page,@RequestParam("limit") int limit){
        int index = (page-1)*limit;
        List<Order> data = orderFeign.findAllByState(0,index,limit);
        int count = orderFeign.findAllByState(0,0,1000).size();
        return new OrderVO(0,"",count,data);
    }

}
